pandas select multiple columns

28

yourdf.drop(['columnheading1', 'columnheading2'], axis=1, inplace=True)
columns = ['b', 'c']
df1 = pd.DataFrame(df, columns=columns)
df = df[["Column_Name1", "Column_Name2"]]
df1 = df.iloc[:, 0:2] # If you want to do it by index. Remember that Python does not slice inclusive of the ending index.
df1 = df[['a', 'b']] ## if you want to do it b nae
#Example, in a df with about 20 columns
#If you want to select columns 1-3, 7, 8, 12-15
# Use numpy's np.r_ to slice the columns and parse it into pandas iloc[] slicer

df.iloc[:, np.r_[1:3, 7, 8, 12:15]]
#This selects all rows "df.iloc[:," and then these selected columns "np.r_[1:3, 7, 8, 12:15]]"
#Remember to import numpy ;)

Comments

Submit
0 Comments